home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Browsers, Managers & Extensions / Mozilla Weave 0.2.7 / latest-weave.xpi / modules / engines / forms.js < prev    next >
Text File  |  2008-07-25  |  8KB  |  252 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Bookmarks Sync.
  15.  *
  16.  * The Initial Developer of the Original Code is Mozilla.
  17.  * Portions created by the Initial Developer are Copyright (C) 2008
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Anant Narayanan <anant@kix.in>
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. const EXPORTED_SYMBOLS = ['FormEngine'];
  38.  
  39. const Cc = Components.classes;
  40. const Ci = Components.interfaces;
  41. const Cu = Components.utils;
  42.  
  43. Cu.import("resource://weave/log4moz.js");
  44. Cu.import("resource://weave/async.js");
  45. Cu.import("resource://weave/util.js");
  46. Cu.import("resource://weave/engines.js");
  47. Cu.import("resource://weave/syncCores.js");
  48. Cu.import("resource://weave/stores.js");
  49. Cu.import("resource://weave/trackers.js");
  50.  
  51. Function.prototype.async = Async.sugar;
  52.  
  53. function FormEngine(pbeId) {
  54.   this._init(pbeId);
  55. }
  56. FormEngine.prototype = {
  57.   __proto__: new SyncEngine(),
  58.  
  59.   get name() { return "forms"; },
  60.   get displayName() { return "Saved Form Data"; },
  61.   get logName() { return "FormEngine"; },
  62.   get serverPrefix() { return "user-data/forms/"; },
  63.  
  64.   __store: null,
  65.   get _store() {
  66.     if (!this.__store)
  67.       this.__store = new FormStore();
  68.     return this.__store;
  69.   },
  70.  
  71.   __core: null,
  72.   get _core() {
  73.     if (!this.__core)
  74.       this.__core = new FormSyncCore(this._store);
  75.     return this.__core;
  76.   },
  77.  
  78.   __tracker: null,
  79.   get _tracker() {
  80.     if (!this.__tracker)
  81.       this.__tracker = new FormsTracker();
  82.     return this.__tracker;
  83.   }
  84. };
  85.  
  86. function FormSyncCore(store) {
  87.   this._store = store;
  88.   this._init();
  89. }
  90. FormSyncCore.prototype = {
  91.   _logName: "FormSync",
  92.   _store: null,
  93.  
  94.   _commandLike: function FSC_commandLike(a, b) {
  95.     /* Not required as GUIDs for similar data sets will be the same */
  96.     return false;
  97.   }
  98. };
  99. FormSyncCore.prototype.__proto__ = new SyncCore();
  100.  
  101. function FormStore() {
  102.   this._init();
  103. }
  104. FormStore.prototype = {
  105.   _logName: "FormStore",
  106.   _lookup: null,
  107.  
  108.   __formDB: null,
  109.   get _formDB() {
  110.     if (!this.__formDB) {
  111.       var file = Cc["@mozilla.org/file/directory_service;1"].
  112.                  getService(Ci.nsIProperties).
  113.                  get("ProfD", Ci.nsIFile);
  114.       file.append("formhistory.sqlite");
  115.       var stor = Cc["@mozilla.org/storage/service;1"].
  116.                  getService(Ci.mozIStorageService);
  117.       this.__formDB = stor.openDatabase(file);
  118.     }
  119.     return this.__formDB;
  120.   },
  121.  
  122.   __formHistory: null,
  123.   get _formHistory() {
  124.     if (!this.__formHistory)
  125.       this.__formHistory = Cc["@mozilla.org/satchel/form-history;1"].
  126.                            getService(Ci.nsIFormHistory2);
  127.     return this.__formHistory;
  128.   },
  129.  
  130.   _createCommand: function FormStore__createCommand(command) {
  131.     this._log.info("FormStore got createCommand: " + command);
  132.     this._formHistory.addEntry(command.data.name, command.data.value);
  133.   },
  134.  
  135.   _removeCommand: function FormStore__removeCommand(command) {
  136.     this._log.info("FormStore got removeCommand: " + command);
  137.  
  138.     var data;
  139.     if (command.GUID in this._lookup) {
  140.       data = this._lookup[command.GUID];
  141.     } else {
  142.       this._log.warn("Invalid GUID found, ignoring remove request.");
  143.       return;
  144.     }
  145.  
  146.     var nam = data.name;
  147.     var val = data.value;
  148.     this._formHistory.removeEntry(nam, val);
  149.  
  150.     delete this._lookup[command.GUID];
  151.   },
  152.  
  153.   _editCommand: function FormStore__editCommand(command) {
  154.     this._log.info("FormStore got editCommand: " + command);
  155.     this._log.warn("Form syncs are expected to only be create/remove!");
  156.   },
  157.  
  158.   wrap: function FormStore_wrap() {
  159.     this._lookup = {};
  160.     var stmnt = this._formDB.createStatement("SELECT * FROM moz_formhistory");
  161.  
  162.     while (stmnt.executeStep()) {
  163.       var nam = stmnt.getUTF8String(1);
  164.       var val = stmnt.getUTF8String(2);
  165.       var key = Utils.sha1(nam + val);
  166.  
  167.       this._lookup[key] = { name: nam, value: val };
  168.     }
  169.  
  170.     return this._lookup;
  171.   },
  172.  
  173.   wipe: function FormStore_wipe() {
  174.     this._formHistory.removeAllEntries();
  175.   },
  176.  
  177.   _resetGUIDs: function FormStore__resetGUIDs() {
  178.     let self = yield;
  179.     // Not needed.
  180.   }
  181. };
  182. FormStore.prototype.__proto__ = new Store();
  183.  
  184. function FormsTracker() {
  185.   this._init();
  186. }
  187. FormsTracker.prototype = {
  188.   _logName: "FormsTracker",
  189.  
  190.   __formDB: null,
  191.   get _formDB() {
  192.     if (!this.__formDB) {
  193.       var file = Cc["@mozilla.org/file/directory_service;1"].
  194.       getService(Ci.nsIProperties).
  195.       get("ProfD", Ci.nsIFile);
  196.       file.append("formhistory.sqlite");
  197.       var stor = Cc["@mozilla.org/storage/service;1"].
  198.       getService(Ci.mozIStorageService);
  199.       this.__formDB = stor.openDatabase(file);
  200.     }
  201.  
  202.     return this.__formDB;
  203.   },
  204.  
  205.   /* nsIFormSubmitObserver is not available in JS.
  206.    * To calculate scores, we instead just count the changes in
  207.    * the database since the last time we were asked.
  208.    *
  209.    * FIXME!: Buggy, because changes in a row doesn't result in
  210.    * an increment of our score. A possible fix is to do a
  211.    * SELECT for each fieldname and compare those instead of the
  212.    * whole row count.
  213.    *
  214.    * Each change is worth 2 points. At some point, we may
  215.    * want to differentiate between search-history rows and other
  216.    * form items, and assign different scores.
  217.    */
  218.   _rowCount: 0,
  219.   get score() {
  220.     var stmnt = this._formDB.createStatement("SELECT COUNT(fieldname) FROM moz_formhistory");
  221.     stmnt.executeStep();
  222.     var count = stmnt.getInt32(0);
  223.     stmnt.reset();
  224.  
  225.     this._score = Math.abs(this._rowCount - count) * 2;
  226.  
  227.     if (this._score >= 100)
  228.       return 100;
  229.     else
  230.       return this._score;
  231.   },
  232.  
  233.   resetScore: function FormsTracker_resetScore() {
  234.     var stmnt = this._formDB.createStatement("SELECT COUNT(fieldname) FROM moz_formhistory");
  235.     stmnt.executeStep();
  236.     this._rowCount = stmnt.getInt32(0);
  237.     stmnt.reset();
  238.     this._score = 0;
  239.   },
  240.  
  241.   _init: function FormsTracker__init() {
  242.     this._log = Log4Moz.Service.getLogger("Service." + this._logName);
  243.     this._score = 0;
  244.  
  245.     var stmnt = this._formDB.createStatement("SELECT COUNT(fieldname) FROM moz_formhistory");
  246.     stmnt.executeStep();
  247.     this._rowCount = stmnt.getInt32(0);
  248.     stmnt.reset();
  249.   }
  250. };
  251. FormsTracker.prototype.__proto__ = new Tracker();
  252.